/**
 * A node implementation for use by a Heap.
 *                                                               
 * @author M. Shearer                                            
 * @author <a href="mailto:shearm@sage.edu">shearm@sage.edu</a>  
 * @version 1.0                                                  
 */

public class HeapNode<E extends Comparable<? super E>> {
    // Data members
    private E   data;
    private int key;
    //values less than current node
    private HeapNode nextLeft;
    //values greater than current node
    private HeapNode nextRight;


    /** Constructs an empty node.
     *
     */
    public HeapNode() {
 this.key = -1;
 this.data     = null;
 nextLeft = null;
    nextRight = null;
    }


    /** Constructs an a node and stores the
     *  specificed element.
     *
     * @param data the element to store.
     */
    public HeapNode( E data ) {
 this.key  = -1;
 this.data = data;
 nextLeft = null;
    nextRight = null;
    }


    /** Constructs an a node and stores the
     *  specificed element and key.
     *
     * @param data the element to store.
     */
    public HeapNode( int key, E data ) {
 this.key  = key;
 this.data = data;
    }
    
    public E getData() {
      return data;
    }


    /** Get the key assigned to this node.
     *
     */
    public int getKey() {
 return ( key );
    }


    /** Set the key assigned to this node.
     *
     * @param key the key for this node.
     */
    public void setKey( int key ) {
 this.key = key;
    }
    
    // returns reference to the next left object
  public HeapNode getNextLeft()
  {
    return nextLeft ;
  }
  
  //returns reference to the next right object
  public HeapNode<E> getNextRight() {
    return ( nextRight );
  }
  
  //sets the value of the next left node
  public void setNextLeft( HeapNode node ) {
    nextLeft = node;
  }
  
  //sets the value of the next right node
  public void setNextRight( HeapNode node ) {
    nextRight = node;
  }


    /** Compares two HeapNodes based on the key.
     *
     */
    public int compareTo( HeapNode<E> hn ) {
 if( this.key < hn.key )
     return ( -1 );
 else {
     if( this.key == hn.key )
  return ( 0 );
     else
  return ( 1 );
 }
    }


    /**
     * Returns the data as a String.
     *
     * @return the internal data as a String.
     */
    public String toString() {
        return ( "<" + key + ", " + data.toString() + ">" );
    }


    /**
     * Testing routine for the class.
     *                               
     * @param args Array of String arguments.
     */                                      
    public static void main( String[] args ) {
        System.out.println( "Hello from HeapNode" );

 HeapNode<String> hn1 = new HeapNode<String>( 2, "Hello" );
 HeapNode<String> hn2 = new HeapNode<String>( 0, "Goodbye" );

 System.out.println( hn1 );
 System.out.println( hn2 );
 System.out.println( hn1.compareTo( hn2 ) );
 System.out.println( hn2.compareTo( hn1 ) );
    }
}